package com.xinli.business.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinli.business.domain.FtMember;
import com.xinli.business.service.IFtMemberService;
import com.xinli.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xinli.common.annotation.Log;
import com.xinli.common.core.controller.BaseController;
import com.xinli.common.core.domain.AjaxResult;
import com.xinli.common.enums.BusinessType;
import com.xinli.business.domain.ConsultantApplication;
import com.xinli.business.service.IConsultantApplicationService;
import com.xinli.common.utils.poi.ExcelUtil;
import com.xinli.common.core.page.TableDataInfo;

/**
 * 咨询师入驻申请Controller
 *
 * @author ruoyi
 * @date 2025-04-28
 */
@RestController
@RequestMapping("/business/application")
public class ConsultantApplicationController extends BaseController
{
    @Autowired
    private IConsultantApplicationService consultantApplicationService;


    @Autowired
    private IFtMemberService ftMemberService;

    /**
     * 查询咨询师入驻申请列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ConsultantApplication consultantApplication)
    {
        startPage();
        List<ConsultantApplication> list = consultantApplicationService.selectConsultantApplicationList(consultantApplication);
        return getDataTable(list);
    }
    /**
     * 查询咨询师入驻申请列表
     */
    @GetMapping("/alllistApplication")
    public AjaxResult alllistApplication(ConsultantApplication consultantApplication)
    {
        List<ConsultantApplication> list = consultantApplicationService.selectConsultantApplicationList(consultantApplication);
        return AjaxResult.success(list);
    }


    /**
     * 导出咨询师入驻申请列表
     */
    @Log(title = "咨询师入驻申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ConsultantApplication consultantApplication)
    {
        List<ConsultantApplication> list = consultantApplicationService.selectConsultantApplicationList(consultantApplication);
        ExcelUtil<ConsultantApplication> util = new ExcelUtil<ConsultantApplication>(ConsultantApplication.class);
        util.exportExcel(response, list, "咨询师入驻申请数据");
    }

    /**
     * 获取咨询师入驻申请详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(consultantApplicationService.selectConsultantApplicationById(id));
    }

    /**
     * 新增咨询师入驻申请
     */
    @Log(title = "咨询师入驻申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ConsultantApplication consultantApplication)
    {
        return toAjax(consultantApplicationService.save(consultantApplication));
    }

    /**
     * 修改咨询师入驻申请
     */
    @Log(title = "咨询师入驻申请", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody ConsultantApplication vo)
    {
        Long userId = vo.getUserId();
        FtMember member = ftMemberService.selectFtMemberByMemberId(userId);
        // 设置身份
        String sf = vo.getSf();
        if(!StringUtils.isEmpty(sf)){
            member.setConsultantTitle(sf);
        }
        // 更新用户信息
        consultantApplicationService.updateMeneberInfo(vo,member);
        // 设置入驻时间
        consultantApplicationService.setConsultantTime(member);
        ftMemberService.updateFtMember(member);
        return toAjax(consultantApplicationService.updateById(vo));
    }

    /**
     * 删除咨询师入驻申请
     */
    @Log(title = "咨询师入驻申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(consultantApplicationService.removeByIds(Arrays.asList(ids)));
    }
}
